package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ciwong.epaper.util.download.ProgressCircle;
import com.ciwong.epaper.R;
import com.ciwong.epaper.bean.Direction;

import java.util.List;

/**
 * 
 * 正在下载中的资源
 * 
 * Created by res-jianpingliu on 2015/7/24.
 */
public class LoadingFileAdapter extends BaseAdapter
{
    private List<Direction> mList;

    private LayoutInflater mInflater;

    public LoadingFileAdapter(Context context, List<Direction> directionList)
    {
        mInflater = LayoutInflater.from(context);
        mList = directionList;
    }

    @Override
    public int getCount()
    {
        if (mList != null && !mList.isEmpty())
        {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position)
    {
        if (mList != null && !mList.isEmpty())
        {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LoadingFileItem item;
        if (convertView == null)
        {
            convertView = mInflater
                    .inflate(R.layout.adapter_loading_item, null);
            item = new LoadingFileItem();
            item.titleTv = (TextView) convertView
                    .findViewById(R.id.loading_item_title_tv);
            item.minPb = (ProgressCircle) convertView
                    .findViewById(R.id.loading_item_min_pb);
            item.horPb = (ProgressBar) convertView
                    .findViewById(R.id.loading_item_hor_pb);
            item.stageTv = (TextView) convertView
                    .findViewById(R.id.loading_item_stage_tv);
            convertView.setTag(item);
        }
        else
        {
            item = (LoadingFileItem) convertView.getTag();
        }

        Direction direction = mList.get(position);
        item.titleTv.setText(direction.getTitle());
        item.stageTv.setText(direction.getStage());
        item.horPb.setProgress(direction.getProgress());
        item.minPb.updateDownloadProgress(direction.getProgress() / 100f);
        return convertView;
    }

    private class LoadingFileItem
    {
        TextView titleTv;

        TextView stageTv;

        ProgressBar horPb;

        ProgressCircle minPb;
    }
}
